package com.dora.sharding.month.module.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dora.sharding.month.module.entity.User;
import com.dora.sharding.month.module.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ACGkaka
 * @since 2021-04-25
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private UserService userService;

    @GetMapping("/create")
    public void create(@RequestBody List<User> users) {
        if (!users.isEmpty()) {
            userService.saveBatch(users);
        } else {
            List<User> userList = new ArrayList<>(3);
            LocalDateTime time1 = LocalDateTime.now();
            LocalDateTime time2 = LocalDateTime.now().plusHours(1);
            userList.add(new User("张三", "123456", 10, time1, time1));
            userList.add(new User("李四", "123456", 11, time2, time2));
            userService.saveBatch(userList);
        }
    }

    @PostMapping("/delete")
    public void delete(@RequestBody String id) {
        userService.removeById(id);
    }

    @PostMapping("/update")
    public void update(@RequestBody User user) {
        userService.updateById(user);
    }

    @GetMapping("/list")
    public List<User> list() {
        return userService.list();
    }

    @GetMapping("/pagehelper/page")
    public PageInfo<User> pageHelperPage() {
        PageHelper.startPage(1, 1);
        List<User> list = userService.list();
        return new PageInfo<>(list);
    }

    @GetMapping("/page")
    public Page<User> mybatisPlusPage() {
        return userService.page(new Page<>(1, 1));
    }
}
